package java_thinking.unit_14._14_8;

import java.util.ArrayList;

class Person {
    public final String first;
    public final String last;
    public final String address;

    Person(String first, String last, String address) {
        this.first = first;
        this.last = last;
        this.address = address;
    }
    @Override
    public String toString() {
        return "Person: "+first+" "+last+" "+address;
    }

    /**
     * 嵌套类 使用static修饰关闭与外围类的关联，相对独立的存在
     */
    public static class NullPerson extends Person implements Null {
        private NullPerson() {
            super("None","None","None");
        }
        @Override
        public String toString(){
            return "NullPerson";
        }
    }

    /**
     * singlemode，为什么使用单例:
     * 可以使用equals()甚至==来与Person.NULL来进行比较
     */
    public static final Person NULL = new NullPerson();
}

class Position {
    private String title;
    private Person person;

    public Position(String jobTitle, Person employee) {
        super();
        title = jobTitle;
        person = employee;
        if (person == null)
            person = Person.NULL;
    }

    public Position(String jobTitle) {
        super();
        title = jobTitle;
        person = Person.NULL;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        if (person == null)
            person = Person.NULL;
    }

    @Override
    public String toString() {
        return "Position [title=" + title + ", person=" + person + "]";
    }

}
class Staff extends ArrayList<Position> {
    public void add(String title, Person person) {
        add(new Position(title, person));
    }

    public void add(String... titles) {
        for (String title : titles) {
            add(new Position(title));
        }
    }

    public Staff(String... titles) {
        add(titles);
    }

    public boolean positionAvailable(String title) {
        for (Position position : this) {
            if (position.getTitle().equals(title) && position.getPerson() == Person.NULL)
                return true;
        }
        return false;
    }

    public void fillPosition(String title, Person person) {
        forEach(position -> {
            if (position.getTitle().equals(title) && position.getPerson() == Person.NULL)
                position.setPerson(person);
            return;
        });
    }
}
